package org.ithang.tools.timer;

import org.ithang.application.bcAddr.service.BcAddrService;
import org.ithang.application.bcRecord.service.BcRecordService;
import org.ithang.application.btAddr.service.BtAddrService;
import org.ithang.application.btRecord.service.BtRecordService;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.eth.ETHWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;

@JobHandler(value="webSocketChecker")
@Component
public class WebSocketChecker extends IJobHandler {

	@Autowired
	private BcAddrService bcAddrService;
	
	@Autowired
	private BtAddrService btAddrService;
	
	@Autowired
	private BtRecordService btRecordService;
	
	@Autowired
	private BcRecordService bcRecordService; 
	
	@Autowired
	private KeyvalueService keyvalueService;
	
	private ETHWebSocket ethWebSocket=null;
	
	@Override
	public ReturnT<String> execute(String param) {
		try{
			
			if(null==ethWebSocket){
				ethWebSocket=new ETHWebSocket(); 
				ethWebSocket.setBcAddrService(bcAddrService);
				ethWebSocket.setBcRecordService(bcRecordService);
				ethWebSocket.setBtAddrService(btAddrService);
				ethWebSocket.setBtRecordService(btRecordService);
				ethWebSocket.setKeyvalueService(keyvalueService);
				ethWebSocket.connect();
			}
			
			if(ethWebSocket.isClose()){
				ethWebSocket=new ETHWebSocket(); 
				ethWebSocket.setBcAddrService(bcAddrService);
				ethWebSocket.setBcRecordService(bcRecordService);
				ethWebSocket.setBtAddrService(btAddrService);
				ethWebSocket.setBtRecordService(btRecordService);
				ethWebSocket.setKeyvalueService(keyvalueService);
				ethWebSocket.connect();
			}
			
			if(ethWebSocket.isClosed()){
				ethWebSocket.connect();
			}
			
			if(ethWebSocket.isOpen()){
				ethWebSocket.send("{\"ping\":\"1\"}");
				//System.out.println("websocket ping");
			}else{
				ethWebSocket.connect();
			}
		}catch(Exception e){
			ETHWebSocket ethWebSocket=new ETHWebSocket(); 
			ethWebSocket.setBcAddrService(bcAddrService);
			ethWebSocket.setBcRecordService(bcRecordService);
			ethWebSocket.setBtAddrService(btAddrService);
			ethWebSocket.setBtRecordService(btRecordService);
			ethWebSocket.setKeyvalueService(keyvalueService);
			ethWebSocket.connect();
			System.out.println("error:"+e.getMessage());
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	public void setEthWebSocket(ETHWebSocket ethWebSocket) {
		this.ethWebSocket = ethWebSocket;
	}

	public BcAddrService getBcAddrService() {
		return bcAddrService;
	}

	public void setBcAddrService(BcAddrService bcAddrService) {
		this.bcAddrService = bcAddrService;
	}

	public BtAddrService getBtAddrService() {
		return btAddrService;
	}

	public void setBtAddrService(BtAddrService btAddrService) {
		this.btAddrService = btAddrService;
	}

	public BtRecordService getBtRecordService() {
		return btRecordService;
	}

	public void setBtRecordService(BtRecordService btRecordService) {
		this.btRecordService = btRecordService;
	}

	public BcRecordService getBcRecordService() {
		return bcRecordService;
	}

	public void setBcRecordService(BcRecordService bcRecordService) {
		this.bcRecordService = bcRecordService;
	}
	
}
